package yxy.game.pm2.command.passbarrier;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.passbarrier.Monster;
import yxy.game.pm2.module.passbarrier.PassbarrierHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.ArrayList;


/****************************************
 * @name CMD_PassBarrierAgainChallenge
 * @description 斩关夺隘再次挑战
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierAgainChallenge, classtype = PassBarrierMessage.PassBarrierAgainChallengeRequest.class)
public class CMD_PassBarrierAgainChallenge extends Command<PassBarrierMessage.PassBarrierAgainChallengeRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierAgainChallengeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        String code = request.getCode();
        PassBarrierMessage.PassBarrierAgainChallengeResponse.Builder response = PassBarrierMessage.PassBarrierAgainChallengeResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            PassbarrierHandle handle = new PassbarrierHandle();

            //todo 再次挑战
            Result result = handle.battleAgainResponseExecute(playerCode, module, response, code);

            //todo 返回主页面信息
            PassbarrierPlayerModule nextModule = PassbarrierPlayerModule.get(playerCode);
            PassBarrierMessage.PassBarrierInfoMsg.Builder passBarrierInfo = handle.getPassBarrierInfo(nextModule);
            response.setMsgData(passBarrierInfo);

            //todo 刚刚队伍信息
            ArrayList<Monster> monsters = nextModule.getMonsterLinkedHashMap().get(code);
            PassBarrierMessage.PassBarrierAgain.Builder builder = handle.getOneAgainMonsterMsg(code, monsters);
            response.setInfo(builder);

            send(messageSource, response, result);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierAgainChallengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
